/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fwdisp.analyser;

import fwdisp.Main;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import settings.MainSettingsManager;
import settings.collections.elements.EType;

/**
 *
 * @author Admin
 */
public class ZwickauAnalyser extends BasicAnalyser{

    
    private Logger logger;
    
    private void removeNulls(String[] split)
    {
        logger.log(Level.FINE, "removing nulls");
         for (int i=0; i < split.length; i++)
            while (split[i] == "" || split[i] == null)
            {
                for (int j = i; j < split.length - 1; j++)
                {
                    split[j] = split[j+1];
                    //split[j+1] = null;
                }
            }
    }
    
    public ZwickauAnalyser(String input) {

        setEtxt("");
        setEort("");
        setEnr("");
        setInput(input);
        logger = Logger.getLogger(Main.class.toString());
        
        ArrayList<EType> eTypes = MainSettingsManager.getSetting().geteTypeCollection().getETypes();
        for(EType type : eTypes)
            if (input.contains(type.getContains()))
                setEType(type);
        
        if (getEType() != null)
            System.out.println("Found eType: " + getEType().getName() + "(id= " + getEType().getId() + ") ");
        else
        {
            System.out.println("No EType found:( ");
           
        }
        
        String[] split = input.split(" ");

        if (split[1].contains("BMA"))
        {
            split[0] = split[0] + " " + split[1] + " " + split[2] + " " + split[3] + " " + split[4];
            split[0] = split[1] = split[2] = split[3] = split[4] = "";
        }
        else
        {
            split[0] = split[0] + " " + split[1];
            split[0] = split[1] = "";
        }


        removeNulls(split);


        logger.log(Level.FINE, "Entferne HNR");
        for (int i = 0; i < split.length; i++)
            if(split[i].equals("HNR") || split[i].equals("HN"))
            {
                for (int j = i; j < split.length - 1; j++)
                    split[j] = split[j+1];
                split[split.length - 1] = "\0";
            }

        for (int i = 0; i < split.length; i++)
        {
            if (split[i].contains(","))
                split[i] = split[i].replace(",",  "");
        } 
        logger.log(Level.FINE, "Hänge die HNR an die Straße");
        for (int i = 0; i < split.length; i++)
            if( (split[i].contains("STRAßE") || split[i].contains("STR.") ||
                  split[i].contains("GÄSSCHEN") || split[i].contains("WEG") ||
                  split[i].contains("GASSE") ) && split[i + 1].matches("\\d++"))
            {
                split[i] = split[i].concat(" " + split[i + 1]);
                for (int j = i + 1; j < split.length -1; j++)
                    split[j] = split[j+1];
            }

        logger.log(Level.FINE, "Entferne HOTs");
        for (int i = 0; i < split.length; i++)
        {
            if (split[i].equals("HOT") || split[i].equals("HOHENSTEIN") || split[i].equals("HOHENSTEIN-E") || split[i].equals("HOHENSTEIN-ERNSTTHAL"))
                if((i == 0 )||( !split[i-1].equals("AS")))//can cause exception
                    split[i] = "";
        }
        removeNulls(split);

        for (int i = 0; i < split.length; i++)
        {
            if(split[i].contains("[NULL]")) //TODO: sinnlos?!
            {
                split[i] = split[i].replaceAll("\\[NULL\\]", "");
            }
            if(split[i].contains("[EOT]"))
            {
                split[i] = split[i].replaceAll("\\[EOT\\]", "");
            }
        }

        removeNulls(split);

        logger.log(Level.FINE, "Zähle Straßen");
        int streets = 0;
        for (int i = 0; i < split.length; i++)
            if ((split[i].contains("STRAßE") || split[i].contains("STR.") ||
                  split[i].contains("GÄSSCHEN") || split[i].contains("WEG") ||
                  split[i].contains("GASSE") || split[i].contains("A4") ))
            {
                streets ++;
            }

        logger.log(Level.FINE, "Erstelle String mit allen Straßen");
        for (int i=0; i < split.length && streets > 0; i++)
        {
            setEort(getEort() + " " + split[i]);
            if ((split[i].contains("STRAßE") || split[i].contains("STR.") ||
              split[i].contains("GÄSSCHEN") || split[i].contains("WEG") ||
              split[i].contains("GASSE") || split[i].contains("A4") ))
            {
                streets--;
                if(streets != 0)
                    setEort(getEort().concat(" /"));
            }
            split[i] = null;
        }

        removeNulls(split);

        //logger.log(Level.FINE, "Werte ENR aus...");
        
        for (int i = 0; i < split.length && !split[i].contains("ENR") ; i++)
        {
            setEtxt(getEtxt() + " " + split[i]);
            //split[i] = null;
        }
         
        //removeNulls(split);
        /*
        //    String[] nr = split[0].split(":"); */
            setEnr("0");
        
    }
    
    
}
